/**
 * Created by ㅋㅋElsa on 2017/9/5.
 */

layui.define(['jquery', 'layer','paging', 'code','form','element'], function(exports){
    var form = layui.form();
    var index;

    layui.code();
    var $ = layui.jquery,
        paging = layui.paging(),
        layer = layui.layer,
        element = layui.element(); //Tab的切换功能，切换事件监听等，需要依赖element模块

    var therole = "";
    $.ajax({
        type:"GET",
        url:contextPath+"/personal/role",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            therole = data.role;
            if(data.role=="管理员"||data.role=="验船师"){
                $('#add').show(); 
            }
        },
        error: function (e) {

        }
    });    

    paging.init({
        url: contextPath + "/exp/all", //地址
        elem: '#con', //内容容器
        type: 'GET',
        //new
        key: '#s_type',
        value: '#s_value',
        params: { //发送到服务端的参数
        },
        tempElem: '#conTemp', //模块容器
        pageConfig: { //分页参数配置
            elem: '#page', //分页容器
            pageSize: 10 //分页大小
        },
        success: function() { //渲染成功的回调
            if(therole=="管理员"){
                $('.deletee').show();
            }
        },
        fail: function(msg) { //获取数据失败的回调
        },
        complate: function() { //完成的回调
        },
    });

    $('#search').on('click', function() {
        var type = $('#search_type').val();
        var value = $('#search_value').val();
        $('#s_type').attr("value",type);
        $('#s_value').attr("value",value);
        if(value === '' || value.length === 0) {
            layer.msg('请输入关键字！');
            return ;
        }

        paging.get({
            value: value,
            type: type
        });
    });

    $('#showall').on('click', function() {
        $('#search_type').val("");
        $('#search_value').val("");
        $('#s_type').removeAttr("value");
        $('#s_value').removeAttr("value");
        form.render();
        paging.get({
            value: null,
            type: null
        });
    });

    $("body").on("click",".deletee",function(){
        var expnum = $(this).data('id');
        layer.alert('确定删除?',{
            skin: 'layui-layer-molv' //样式类名  自定义样式
            ,closeBtn: 1    // 是否显示关闭按钮
            ,anim: 1 //动画类型
            ,btn: ['确定','取消'] //按钮
            ,icon: 6    // icon
        },function(){
            $.ajax({
                type:"DELETE",
                url:contextPath+"/exp/delete?expnum="+expnum,
                contentType: "application/json; charset=utf-8",
                dataType:"json",
                success:function (data) {
                    console.log(data.message);
                    if(data.message == "经验删除成功!"){
                        layer.msg('经验删除成功!<br>页面将在1秒后刷新...', {
                            time: 1000 //1s后自动关闭
                        });
                        setTimeout(function(){window.location.reload();},1000);
                    } else {
                        layer.msg(data.message, {
                            time:false,
                            btn:"确定",
                            btnAlign: 'c'
                        });
                    }
                },
                error:function (e) {
                }
            });
        });
    });

    $("body").on("click",".moress",function(){
        var expnum = $(this).data('id');
        tampdata = $(this).data('id');

        $.ajax({
            type:"GET",
            url:contextPath+"/exp/info",
            data: {"expnum": expnum },
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            success:function (data) {
                $.get('DetailsExp.html', null, function(DetailsExp) {
                    layer.open({
                        type: 1,
                        title: '经验详情',
                        content: DetailsExp,
                        area: ['auto', '600px'],
                        maxmin: false,
                        success:function (layero,index) {
                            layero.find("#closex").on("click",function () {
                                layer.closeAll();
                            });
                            layero.find("#close").on("click",function () {
                                layer.closeAll();
                            });

                            $('input[name="expname"]').attr("value",data.expname);
                            $('input[name="exptxr"]').attr("value",data.exptxr);
                            $('input[name="exptime"]').attr("value",data.exptime);
                            $('textarea[name="expcontent"]').val(data.expcontent);

                            $.ajax({
                                type:"GET",
                                url:contextPath+"/personal/role",
                                contentType: "application/json; charset=utf-8",
                                dataType: "json",
                                success: function (data) {
                                    console.log(data.role);
                                    if(data.role=="管理员"){
                                        $('#edit').show();
                                    }
                                },
                                error: function (e) {

                                }
                            });

                        },
                        yes: function(index) {
                            console.log(index);
                        },
                        full: function(elem) {
                            var win = window.top === window.self ? window : parent.window;
                            $(win).on('resize', function() {
                                var $this = $(this);
                                elem.width($this.width()).height($this.height()).css({
                                    top: 0,
                                    left: 0
                                });
                                elem.children('div.layui-layer-content').height($this.height() - 95);
                            });
                        }
                    });
                    form.render();
                });
            },
            error:function (e) {
            }
        });
    });

    $('body').on('click', '#close', function(){
        layer.close(index);
        return false;
    });

    $('body').on('click', '#closex', function(){
        layer.close(index);
        return false;
    });

    $('#add').on('click', function() {
        $.get('NewExpList.html', null, function(NewExpList) {
            layer.open({
                type: 1,
                title: '新建经验',
                content: NewExpList,
                area: ['auto', '600px'],
                maxmin: false,
                success:function (layero,index) {
                    layero.find("#closex").on("click",function () {
                        layer.closeAll();
                    });

                    $.ajax({
                        type: "GET",
                        url: contextPath + "/personal/info",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            $('input[name="exptxr"]').attr("value",data.name);
                        },
                        error: function (e) {
                        }
                    });
                    $('input[name="exptime"]').attr("value",getNowFormatDate());

                },
                yes: function(index) {
                    console.log(index);
                },
                full: function(elem) {
                    var win = window.top === window.self ? window : parent.window;
                    $(win).on('resize', function() {
                        var $this = $(this);
                        elem.width($this.width()).height($this.height()).css({
                            top: 0,
                            left: 0
                        });
                        elem.children('div.layui-layer-content').height($this.height() - 95);
                    });
                }
            });
            form.render();
        });
    });


    exports('ExpList', {}); //注意，这里是模块输出的核心，模块名必须和use时的模块名一致
});